package com.hdl.servlet.user;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.mail.MessagingException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hdl.entity.HdlUser;
import com.hdl.service.UserService;
import com.hdl.service.impl.UserServiceImpl;
import com.hdl.util.MailUtil;

/**
 * FindPsdServlet 找回密码
 * @author Administrator
 *
 */
@WebServlet("/findPsd.let")
public class FindPsdServlet extends HttpServlet{

	private UserService service = new UserServiceImpl();
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		//字符编码
		req.setCharacterEncoding("utf-8");
		resp.setContentType("application/json;charset=utf-8");
		
		//获取参数
		String email = req.getParameter("hdlUserEmail");
		
		//调用用户业务逻辑方法
		HdlUser user = service.findPsdByEmail(email);
		ObjectMapper mapper = new ObjectMapper();
		Map<String,Object> map = new HashMap<String,Object>();
		
		if(user == null) {//说明该邮箱不可以对应一个用户
			map.put("code", 1);
			map.put("msg","输入的邮箱不正确");
			mapper.writeValue(resp.getOutputStream(), map);
		}
		
		//发送邮箱
		try {
			MailUtil.sendPsd(email, user.getHdlUserPsd());
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		map.put("code", 2);
		map.put("msg","请在邮箱中查看找回的密码");
		mapper.writeValue(resp.getOutputStream(), map);
		
		
	}
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
}
